home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / content / nsTextFragment.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  7KB  |  266 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla Communicator client code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  26.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37. #ifndef nsTextFragment_h___
  38. #define nsTextFragment_h___
  39.  
  40. #include "nsAString.h"
  41. class nsString;
  42. class nsCString;
  43.  
  44. // XXX should this normalize the code to keep a \u0000 at the end?
  45.  
  46. // XXX nsTextFragmentPool?
  47.  
  48. // XXX these need I18N spankage
  49. #define XP_IS_SPACE(_ch) \
  50.   (((_ch) == ' ') || ((_ch) == '\t') || ((_ch) == '\n'))
  51.  
  52. #define XP_IS_UPPERCASE(_ch) \
  53.   (((_ch) >= 'A') && ((_ch) <= 'Z'))
  54.  
  55. #define XP_IS_LOWERCASE(_ch) \
  56.   (((_ch) >= 'a') && ((_ch) <= 'z'))
  57.  
  58. #define XP_TO_LOWER(_ch) ((_ch) | 32)
  59.  
  60. #define XP_TO_UPPER(_ch) ((_ch) & ~32)
  61.  
  62. #define XP_IS_SPACE_W XP_IS_SPACE
  63.  
  64. /**
  65.  * A fragment of text. If mIs2b is 1 then the m2b pointer is valid
  66.  * otherwise the m1b pointer is valid. If m1b is used then each byte
  67.  * of data represents a single ucs2 character with the high byte being
  68.  * zero.
  69.  *
  70.  * This class does not have a virtual destructor therefore it is not
  71.  * meant to be subclassed.
  72.  */
  73. class nsTextFragment {
  74. public:
  75.   /**
  76.    * Default constructor. Initialize the fragment to be empty.
  77.    */
  78.   nsTextFragment()
  79.     : m1b(nsnull), mAllBits(0)
  80.   {
  81.   }
  82.  
  83.   ~nsTextFragment();
  84.  
  85.   /**
  86.    * Initialize the contents of this fragment to be a copy of
  87.    * the argument fragment.
  88.    */
  89.   nsTextFragment(const nsTextFragment& aOther);
  90.  
  91.   /**
  92.    * Initialize the contents of this fragment to be a copy of
  93.    * the argument 7bit ascii string.
  94.    */
  95.   nsTextFragment(const char *aString);
  96.  
  97.   /**
  98.    * Initialize the contents of this fragment to be a copy of
  99.    * the argument ucs2 string.
  100.    */
  101.   nsTextFragment(const PRUnichar *aString);
  102.  
  103.   /**
  104.    * Initialize the contents of this fragment to be a copy of
  105.    * the argument string.
  106.    */
  107.   nsTextFragment(const nsString& aString);
  108.  
  109.   /**
  110.    * Change the contents of this fragment to be a copy of the
  111.    * the argument fragment.
  112.    */
  113.   nsTextFragment& operator=(const nsTextFragment& aOther);
  114.  
  115.   /**
  116.    * Change the contents of this fragment to be a copy of the
  117.    * the argument 7bit ascii string.
  118.    */
  119.   nsTextFragment& operator=(const char *aString);
  120.  
  121.   /**
  122.    * Change the contents of this fragment to be a copy of the
  123.    * the argument ucs2 string.
  124.    */
  125.   nsTextFragment& operator=(const PRUnichar *aString);
  126.  
  127.   /**
  128.    * Change the contents of this fragment to be a copy of the
  129.    * the argument string.
  130.    */
  131.   nsTextFragment& operator=(const nsAString& aString);
  132.  
  133.   /**
  134.    * Return PR_TRUE if this fragment is represented by PRUnichar data
  135.    */
  136.   PRBool Is2b() const
  137.   {
  138.     return mState.mIs2b;
  139.   }
  140.  
  141.   /**
  142.    * Return PR_TRUE if this fragment contains Bidi text
  143.    * For performance reasons this flag is not set automatically, but
  144.    * requires an explicit call to SetBidiFlag()
  145.    */
  146.   PRBool IsBidi() const
  147.   {
  148.     return mState.mIsBidi;
  149.   }
  150.  
  151.   /**
  152.    * Get a pointer to constant PRUnichar data.
  153.    */
  154.   const PRUnichar *Get2b() const
  155.   {
  156.     NS_ASSERTION(Is2b(), "not 2b text"); 
  157.     return m2b;
  158.   }
  159.  
  160.   /**
  161.    * Get a pointer to constant char data.
  162.    */
  163.   const char *Get1b() const
  164.   {
  165.     NS_ASSERTION(!Is2b(), "not 1b text"); 
  166.     return (const char *)m1b;
  167.   }
  168.  
  169.   /**
  170.    * Get the length of the fragment. The length is the number of logical
  171.    * characters, not the number of bytes to store the characters.
  172.    */
  173.   PRInt32 GetLength() const
  174.   {
  175.     return PRInt32(mState.mLength);
  176.   }
  177.  
  178.   /**
  179.    * Change the contents of this fragment to be the given buffer and
  180.    * length. The memory becomes owned by the fragment. In addition,
  181.    * the memory for aBuffer must have been allocated using the 
  182.    * nsIMemory interface.
  183.    */
  184.   void SetTo(PRUnichar *aBuffer, PRInt32 aLength, PRBool aRelease);
  185.  
  186.   /**
  187.    * Change the contents of this fragment to be a copy of the given
  188.    * buffer. Like operator= except a length is specified instead of
  189.    * assuming 0 termination.
  190.    */
  191.   void SetTo(const PRUnichar* aBuffer, PRInt32 aLength);
  192.  
  193.   /**
  194.    * Change the contents of this fragment to be a copy of the given
  195.    * buffer. Like operator= except a length is specified instead of
  196.    * assuming 0 termination.
  197.    */
  198.   void SetTo(const char *aBuffer, PRInt32 aLength);
  199.  
  200.   /**
  201.    * Append the contents of this string fragment to aString
  202.    */
  203.   void AppendTo(nsAString& aString) const;
  204.  
  205.   /**
  206.    * Append the contents of this string fragment to aCString. This
  207.    * method will do a lossy conversion from UTF-16 to ASCII.
  208.    */
  209.   void AppendTo(nsACString& aCString) const;
  210.  
  211.   /**
  212.    * Make a copy of the fragments contents starting at offset for
  213.    * count characters. The offset and count will be adjusted to
  214.    * lie within the fragments data. The fragments data is converted if
  215.    * necessary.
  216.    */
  217.   void CopyTo(PRUnichar *aDest, PRInt32 aOffset, PRInt32 aCount);
  218.  
  219.   /**
  220.    * Make a copy of the fragments contents starting at offset for
  221.    * count characters. The offset and count will be adjusted to
  222.    * lie within the fragments data. The fragments data is converted if
  223.    * necessary.
  224.    */
  225.   void CopyTo(char *aDest, PRInt32 aOffset, PRInt32 aCount);
  226.  
  227.   /**
  228.    * Return the character in the text-fragment at the given
  229.    * index. This always returns a PRUnichar.
  230.    */
  231.   PRUnichar CharAt(PRInt32 aIndex) const
  232.   {
  233.     NS_ASSERTION(PRUint32(aIndex) < mState.mLength, "bad index");
  234.     return mState.mIs2b ? m2b[aIndex] : PRUnichar(m1b[aIndex]);
  235.   }
  236.  
  237.   /**
  238.    * Scan the contents of the fragment and turn on mState.mIsBidi if it
  239.    * includes any Bidi characters.
  240.    */
  241.   void SetBidiFlag();
  242.  
  243.   struct FragmentBits {
  244.     PRBool mInHeap : 1;
  245.     PRBool mIs2b : 1;
  246.     PRBool mIsBidi : 1;
  247.     PRUint32 mLength : 29;
  248.   };
  249.  
  250. protected:
  251.   void ReleaseText();
  252.  
  253.   union {
  254.     const PRUnichar *m2b;
  255.     const unsigned char *m1b;
  256.   };
  257.  
  258.   union {
  259.     PRUint32 mAllBits;
  260.     FragmentBits mState;
  261.   };
  262. };
  263.  
  264. #endif /* nsTextFragment_h___ */
  265.  
  266.